using System;

namespace ZFramework.Runtime
{
    public abstract class ObjectBase : IReference
    {
        private string _name;
        private object _instance;
        private bool _locked;
        private DateTime _lastUseTime;
        private int _priority;

        public ObjectBase()
        {
            _name = null;
            _instance = null;
            _locked = false;
            _lastUseTime = default(DateTime);
            _priority = 0;
        }

        public string Name => _name;

        public object Instance => _instance;

        public DateTime LastUseTime
        {
            get => _lastUseTime;
            set => _lastUseTime = value;
        }
        
        /// <summary>
        /// 获取或设置对象的优先级。
        /// </summary>
        public int Priority
        {
            get
            {
                return _priority;
            }
            set
            {
                _priority = value;
            }
        }
        
        /// <summary>
        /// 获取或设置对象是否被加锁。
        /// </summary>
        public bool Locked
        {
            get
            {
                return _locked;
            }
            set
            {
                _locked = value;
            }
        }

        //初始化
        protected void Initialize(string name, object obj)
        {
            _name = name;
            _instance = obj;
            _lastUseTime = DateTime.UtcNow;
        }

        public virtual void Clear()
        {
            _name = null;
            _instance = null;
            _lastUseTime = default(DateTime);
        }

        //从池子中取出
        public virtual void OnSpawn()
        {

        }

        //放回池子中
        public virtual void OnUnSpawn()
        {

        }

        //销毁
        public abstract void Release();


    }
}
